import fs from 'fs';
import lodash from 'lodash';
import { createRequire } from 'module';
import path, { dirname } from 'path';
import prettier from 'prettier';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const require = createRequire(import.meta.url);
const cwd = process.cwd();
const prettierrc = JSON.parse(
  fs.readFileSync(path.resolve(cwd, '.prettierrc'), 'utf8'),
);

let components = [
  { value: 'Button', label: '按钮' },
  { value: 'Cell', label: '单元格' },
  { value: 'ConfigProvider', label: '全局配置' },
  { value: 'Icon', label: '图标' },
  { value: 'Image', label: '图片' },
  { value: 'Row', label: '行布局' },
  { value: 'Col', label: '列布局' },
  { value: 'Popup', label: '弹出层' },
  { value: 'Space', label: '间距' },
  // { value: 'Style', label: '内置样式' },
  { value: 'Toast', label: '轻提示' },
  { value: 'Calendar', label: '日历' },
  { value: 'Cascader', label: '级联选择' },
  { value: 'Checkbox', label: '复选框' },
  { value: 'DatePicker', label: '日期选择' },
  { value: 'Field', label: '输入框' },
  { value: 'Form', label: '表单' },
  { value: 'NumberKeyboard', label: '数字键盘' },
  { value: 'PasswordInput', label: '密码输入框' },
  { value: 'Picker', label: '选择器' },
  { value: 'PickerGroup', label: '选择器组' },
  { value: 'Radio', label: '单选框' },
  { value: 'Rate', label: '评分' },
  { value: 'Search', label: '搜索' },
  { value: 'Slider', label: '滑块' },
  { value: 'Signature', label: '签名' },
  { value: 'Stepper', label: '步进器' },
  { value: 'Switch', label: '开关' },
  { value: 'TimePicker', label: '时间选择' },
  { value: 'Uploader', label: '文件上传' },
  { value: 'ActionSheet', label: '动作面板' },
  { value: 'Barrage', label: '弹幕' },
  { value: 'Dialog', label: '弹出框' },
  { value: 'DropdownMenu', label: '下拉菜单' },
  { value: 'FloatingPanel', label: '浮动面板' },
  { value: 'FloatingBubble', label: '浮动气泡' },
  { value: 'Loading', label: '加载' },
  { value: 'Notify', label: '消息通知' },
  { value: 'Overlay', label: '遮罩层' },
  { value: 'PullRefresh', label: '下拉刷新' },
  { value: 'ShareSheet', label: '分享面板' },
  { value: 'SwipeCell', label: '滑动单元格' },
  { value: 'Badge', label: '徽标' },
  { value: 'Circle', label: '环形进度条' },
  { value: 'Collapse', label: '折叠面板' },
  { value: 'CountDown', label: '倒计时' },
  { value: 'Divider', label: '分割线' },
  { value: 'Empty', label: '空状态' },
  { value: 'ImagePreview', label: '图片预览' },
  { value: 'Lazyload', label: '懒加载' },
  { value: 'List', label: '列表' },
  { value: 'NoticeBar', label: '通知栏' },
  { value: 'Popover', label: '气泡弹出框' },
  { value: 'Progress', label: '进度条' },
  { value: 'RollingText', label: '翻滚文本' },
  { value: 'Skeleton', label: '骨架屏' },
  { value: 'Steps', label: '步骤条' },
  { value: 'Sticky', label: '粘性布局' },
  { value: 'Swipe', label: '轮播' },
  { value: 'Tag', label: '标签' },
  { value: 'TextEllipsis', label: '文本省略' },
  { value: 'Watermark', label: '水印' },
  { value: 'ActionBar', label: '动作栏' },
  { value: 'BackTop', label: '回到顶部' },
  { value: 'Grid', label: '宫格' },
  { value: 'IndexBar', label: '索引栏' },
  { value: 'NavBar', label: '导航栏' },
  { value: 'Pagination', label: '分页' },
  { value: 'Sidebar', label: '侧边导航' },
  { value: 'Tab', label: '标签页' },
  { value: 'Tabbar', label: '标签栏' },
  { value: 'TreeSelect', label: '分类选择' },
  { value: 'AddressEdit', label: '地址编辑' },
  { value: 'AddressList', label: '地址列表' },
  { value: 'Area', label: '省市区选择' },
  { value: 'Card', label: '商品卡片' },
  { value: 'ContactCard', label: '联系人卡片' },
  { value: 'ContactEdit', label: '联系人编辑' },
  { value: 'ContactList', label: '联系人列表' },
  { value: 'Coupon', label: '优惠券' },
  { value: 'SubmitBar', label: '提交订单栏' },
];
components = fs
  .readdirSync(path.resolve(cwd, 'components/mobile/_basic'), {
    encoding: 'utf-8',
  })
  .filter(
    v =>
      !['utils', 'composables', 'locale', 'style'].includes(v) &&
      fs
        .statSync(path.resolve(cwd, 'components/mobile/_basic', v))
        .isDirectory(),
  );

let mobileDir = path.resolve(cwd, 'components/mobile');
let projectDir = path.resolve(mobileDir, 'basic');
let r = '';

components.forEach((v, i, a) => {});

for (let i = 0; i < components.length; i++) {
  let value = components[i];
  let kebabCaseName = lodash.kebabCase(value);
  let capitalizeName = lodash.capitalize(value);
  let camelCaseName = lodash.camelCase(value);
  let snakeCaseName = lodash.snakeCase(value);
  let startCaseName = lodash.startCase(value);
  let ComponentsCaseName = lodash.upperFirst(camelCaseName);
  let toPath = path.resolve(projectDir, kebabCaseName);

  fs.rmSync(toPath, { recursive: true, force: true });
  fs.mkdirSync(toPath);

  let writeFilePath = path.resolve(toPath, 'index.tsx');
  let r = `import type { App } from 'vue';
import Component    from './Component';

// ${ComponentsCaseName}
export * from './Component';
export type * from './interface';
export const version = '0.0.0';

export default Object.assign(Component, {
  install: (app: App, options) => {
    app.component(Component.name, Component);
  },
});
`;
  if (ComponentsCaseName === 'Cascader') {
    r += `export * from 'tc/data/area.mjs';`;
  }
  r = await prettier.format(r, { ...prettierrc, parser: 'babel-ts' });
  fs.writeFileSync(writeFilePath, r, { encoding: 'utf-8' });

  let b = fs.existsSync(
    path.resolve(cwd, `components/mobile/_basic/${kebabCaseName}/types.ts`),
  );
  r = `export { };`;
  writeFilePath = path.resolve(toPath, 'interface.ts');
  if (b) {
    r = `export * from '../../_basic/${kebabCaseName}/types';
  ${r}`;
  } else {
    console.log(`${value}`, '没有 types 文件');
  }
  r = await prettier.format(r, { ...prettierrc, parser: 'babel-ts' });
  fs.writeFileSync(writeFilePath, r, { encoding: 'utf-8' });

  writeFilePath = path.resolve(toPath, 'Component.tsx');
  r = `import {
  commonProps,
   upperFirstProps, upperFirstPropsMapping,pickOwnForBasicComponent,
} from 'components/mobile/_utils';
import Component    from '../../_basic/${kebabCaseName}';
import { defineComponent } from 'vue';
import './index.scss';

export * from '../../_basic/${kebabCaseName}';

export default defineComponent({
  name: Component.name,
  props: {

  },
  emits: [],

  setup(props, { attrs, emit, expose, slots }) {
    // expose({});

    return () => {
      return (
        <Component v-show={mappingProps.visible} {...attrs} {...upperFirstPropsMapping(props)}>
          {slots}
        </Component>
      );
    };
  },
});
`;
  r = await prettier.format(r, { ...prettierrc, parser: 'babel-ts' });
  fs.writeFileSync(writeFilePath, r, { encoding: 'utf-8' });

  writeFilePath = path.resolve(toPath, 'index.scss');
  fs.writeFileSync(writeFilePath, '', { encoding: 'utf-8' });

  let files = fs.readdirSync(toPath, {
    encoding: 'utf-8',
    withFileTypes: false,
    recursive: true,
  });
  files.forEach(n => {
    const f = path.join(toPath, n);
    const stats = fs.statSync(f);
    const isFile = stats.isFile();
    const isDirectory = stats.isDirectory();

    let t = lodash.kebabCase(n);
    if (fs.existsSync(f)) {
    }
    if (isFile) {
    }
    if (isDirectory) {
    }
  });
}
